package com.yikome.product.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import com.yikome.common.CommonService;
import com.yikome.product.entity.Supplier;
import com.yikome.product.repository.SupplierRepository;
import com.yikome.product.request.SupplierRequest;

import cn.hutool.core.util.StrUtil;

@Service
public class SupplierService extends CommonService<Supplier, Long> {

	@Autowired
	private SupplierRepository supplierRepository ;

	@Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setResourceDao(SupplierRepository repository) {
		super.setCommonDao(repository);
	}
	
	public List<Map<String , Object>> findProduct(SupplierRequest supplierRequest) {
		
		String contentSQL = "SELECT '' AS 'id' , '全部供应商' AS 'text' FROM DUAL \n" + 
							"UNION \n" + 
							"SELECT s_supplier.id , s_supplier.shop_name FROM s_supplier \n" ;
		
		Map<String , Object> paramsMap = new HashMap<>();
    	StringBuilder paramsSQL = new StringBuilder("WHERE s_supplier.yn = 1 \n") ;
    	
    	if(supplierRequest.getId() != null) {
    		paramsSQL.append("AND s_supplier.id = :supplierId \n");
    		paramsMap.put("supplierId", supplierRequest.getId());
    	}
    	if(StrUtil.isNotBlank(supplierRequest.getName())) {
    		paramsSQL.append("AND s_supplier.shop_name Like :supplierName \n");
    		paramsMap.put("supplierName", "%" + supplierRequest.getName() + "%");
    	}
		
		return jdbcTemplate.queryForList(contentSQL, paramsMap);
	} 
}
